use Getopt::Long;
use Cwd 'abs_path';
$line = abs_path($0);
chomp $line;
@DR_array = split('/',$line);
pop(@DR_array);
$dir = join("/",@DR_array);

#reading input arguments
&Getopt::Long::GetOptions(
'REF_FILE=s' => \$reffile,
'INPUT_FILE_LIST=s' => \$inputfilelist,
'TEMP=s' => \$temp,
'OUTPUT=s' => \$output
);
$email="prodduturi.naresh@mayo.edu";
$mem="3G";
$queue="1-hour";
chomp($inputfilelist);
chomp($reffile);
chomp($temp);
chomp($output);
#checking for missing arguments
if($output eq "" | $reffile eq "" || $inputfilelist eq "" ||  $temp eq "" )
{
	die "missing arguments\n USAGE : perl perl_merge.pl -OUTPUT <OUTPUT.txt> -INPUT_FILE_LIST <INPUT FILE LIST> -REF_FILE <REF FILE> -TEMP <TEMP DIRECTORY>\n";
}

#create temp directory unless it is created
unless(-d $temp)
{
    system("mkdir -p $temp");
}

print "***********INPUT ARGUMENTS***********\n";
print "REF_FILE: $reffile\n";
print "INPUT_FILE_LIST: $inputfilelist\n";
print "TEMP : $temp\n";
print "OUTPUT : $output\n\n\n\n\n";
open(WRBUFF,">$temp/file_job_submit");
open(BUFF,"$inputfilelist") or die "no file $inputfilelist exists\n";
$num=0;
$paste = "paste";
while($file=<BUFF>)
{
	$num++;
	chomp($file);
	print WRBUFF "-INPUT_FILE $file -REF_FILE  $reffile -OUTPUT_FILE $temp/out_$num.txt\n";
	$paste=$paste." $temp/out_$num.txt";
}
$paste=$paste."> $temp/out.txt";
close(BUFF);
#prepaing the main job
open(WRBUFF,">$temp/file_job_submit.csh");
$com = '#$';
print WRBUFF "$com -q $queue\n";
print WRBUFF "$com -l h_vmem=$mem\n";
print WRBUFF "$com -t 1-$num:1\n";
print WRBUFF "$com -M $email\n";
print WRBUFF "$com -m a\n";
print WRBUFF "$com -V\n";
print WRBUFF "$com -cwd\n";
print WRBUFF "$com -e $temp/\n";
print WRBUFF "$com -o $temp/\n";
$tmp = "perl $dir/perl_create_reffile.pl ".'`cat  '."$temp/file_job_submit".' |head -${SGE_TASK_ID} |tail -1`'."\n";
print WRBUFF "$tmp\n";
close(WRBUFF);
$sys = "/home/sge6_2/bin/lx24-amd64/qsub $temp/file_job_submit.csh > $temp/jobid_shapeit";
system($sys);
#extractingt he job id
open(WRBUFF,"$temp/jobid_shapeit") or die "unable to open file $temp/jobid_shapeit\n";
$shapeit = <WRBUFF>;
print "$shapeit\n";
@shapeit =split(" ",$shapeit);
@shapeit1 =split(/\./,$shapeit[2]);
print "JOB ID $shapeit1[0]\n";
$WRBUFF = $shapeit1[0];

##prepaing the waiting job
open(WRBUFF,">$temp/ArrayJob_shapeit_wait.csh") or die "unable to create the array job wait file shape it \n";
print WRBUFF '#! /bin/bash'."\n";
print WRBUFF '#$ -q 1-hour'."\n";
print WRBUFF '#$ -l h_vmem=2G'."\n";
print WRBUFF '#$ -M $email'."\n";
print WRBUFF '#$ -m a'."\n";
print WRBUFF '#$ -V'."\n";
print WRBUFF '#$ -cwd'."\n";
$com = '#$';
print WRBUFF "$com -e $temp/\n";
print WRBUFF "$com -o $temp/\n";

print WRBUFF "cp $temp/jobid_shapeit $temp/waiting.txt\n";
$sys = "/home/sge6_2/bin/lx24-amd64/qsub -hold_jid $WRBUFF $temp/ArrayJob_shapeit_wait.csh\n";
system($sys);
$flag = 1;
while($flag > 0)
{
	if($flag%100000000 == 0)
	{
			$flag = 1;
			print "waiting for the main job to complete\n";
	}
	$flag++;
	if(-e "$temp/waiting.txt")	
	{
			$flag =0;
	}
}

#merging all the output file
system($paste);
system("rm $temp/out_*.txt");

#creating the output file
open(BUFF,"$temp/out.txt") or die "no file found $temp/out\n";
open(WRBUFF,">$output") or die "not able to write the file\n";
while(<BUFF>)
{
	chomp($_);
	@array=split("\t",$_);
	print WRBUFF $array[0]."\t".$array[1];
	for($i=2;$i<@array;$i++)
	{	$i++;
		print WRBUFF "\t$array[$i]";
	}
	print WRBUFF "\n";
}
close(BUFF);
close(WRBUFF);

#removing the temp files
system("rm $temp/*");
system("rmdir $temp");
